package com.baweiwei.ordersapi.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baweiwei.common.entity.Good;
import com.baweiwei.common.entity.OrderDetail;
import com.baweiwei.common.entity.Orders;
import com.baweiwei.common.entity.Users;
import com.baweiwei.ordersapi.dao.OrdersDao;
import com.baweiwei.ordersapi.feign.GoodFeign;
import com.baweiwei.ordersapi.feign.UserFeign;
import com.baweiwei.ordersapi.service.OrdersService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private GoodFeign goodFeign;

    @Autowired
    private UserFeign userFeign;

    @Override
    @Transactional
    public void addOrder(Orders orders) {
        ///////////////////以上是模拟的数据//////////////////////////////
        //保存订单
        ordersDao.save(orders);
        //减库存
        List<OrderDetail> orderDetails = orders.getOrderDetails();
        for(OrderDetail orderDetail:orderDetails){
            //商品的数量
            Integer num = orderDetail.getNum();
            //商品ID
            Integer goodId = orderDetail.getGood().getGoodId();
            goodFeign.kuncun(goodId,num);
        }

       // int i=1/0;
        //金额
        Integer userId = orders.getUsers().getId();
        BigDecimal total = orders.getTotal();
        userFeign.money(userId,total);
    }
}
